<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.4.0-alpha - src/lmathlib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lmathlib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.4.0-alpha</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">234</td>
            <td class="headerCovTableEntry">236</td>
            <td class="headerCovTableEntryHi">99.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:19</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lmathlib.c $
<span class="lineNum">       3 </span>            : ** Standard mathematical library
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #define lmathlib_c
<span class="lineNum">       8 </span>            : #define LUA_LIB
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;lprefix.h&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #include &lt;float.h&gt;
<span class="lineNum">      14 </span>            : #include &lt;limits.h&gt;
<span class="lineNum">      15 </span>            : #include &lt;math.h&gt;
<span class="lineNum">      16 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      17 </span>            : #include &lt;time.h&gt;
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      22 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      23 </span>            : 
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : #undef PI
<span class="lineNum">      26 </span>            : #define PI      (l_mathop(3.141592653589793238462643383279502884))
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span>            : 
<span class="lineNum">      29 </span><span class="lineCov">          6 : static int math_abs (lua_State *L) {</span>
<span class="lineNum">      30 </span><span class="lineCov">          6 :   if (lua_isinteger(L, 1)) {</span>
<span class="lineNum">      31 </span><span class="lineCov">          3 :     lua_Integer n = lua_tointeger(L, 1);</span>
<span class="lineNum">      32 </span><span class="lineCov">          3 :     if (n &lt; 0) n = (lua_Integer)(0u - (lua_Unsigned)n);</span>
<span class="lineNum">      33 </span><span class="lineCov">          3 :     lua_pushinteger(L, n);</span>
<span class="lineNum">      34 </span>            :   }
<span class="lineNum">      35 </span>            :   else
<span class="lineNum">      36 </span><span class="lineCov">          3 :     lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      37 </span><span class="lineCov">          6 :   return 1;</span>
<span class="lineNum">      38 </span>            : }
<span class="lineNum">      39 </span>            : 
<span class="lineNum">      40 </span><span class="lineCov">          1 : static int math_sin (lua_State *L) {</span>
<span class="lineNum">      41 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      42 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      43 </span>            : }
<span class="lineNum">      44 </span>            : 
<span class="lineNum">      45 </span><span class="lineCov">          1 : static int math_cos (lua_State *L) {</span>
<span class="lineNum">      46 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      47 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      48 </span>            : }
<span class="lineNum">      49 </span>            : 
<span class="lineNum">      50 </span><span class="lineCov">          1 : static int math_tan (lua_State *L) {</span>
<span class="lineNum">      51 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      52 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      53 </span>            : }
<span class="lineNum">      54 </span>            : 
<span class="lineNum">      55 </span><span class="lineCov">          1 : static int math_asin (lua_State *L) {</span>
<span class="lineNum">      56 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      57 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      58 </span>            : }
<span class="lineNum">      59 </span>            : 
<span class="lineNum">      60 </span><span class="lineCov">          1 : static int math_acos (lua_State *L) {</span>
<span class="lineNum">      61 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">      62 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      63 </span>            : }
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span><span class="lineCov">          2 : static int math_atan (lua_State *L) {</span>
<span class="lineNum">      66 </span><span class="lineCov">          2 :   lua_Number y = luaL_checknumber(L, 1);</span>
<span class="lineNum">      67 </span><span class="lineCov">          2 :   lua_Number x = luaL_optnumber(L, 2, 1);</span>
<span class="lineNum">      68 </span><span class="lineCov">          2 :   lua_pushnumber(L, l_mathop(atan2)(y, x));</span>
<span class="lineNum">      69 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">      70 </span>            : }
<span class="lineNum">      71 </span>            : 
<span class="lineNum">      72 </span>            : 
<span class="lineNum">      73 </span><span class="lineCov">         59 : static int math_toint (lua_State *L) {</span>
<span class="lineNum">      74 </span>            :   int valid;
<span class="lineNum">      75 </span><span class="lineCov">         59 :   lua_Integer n = lua_tointegerx(L, 1, &amp;valid);</span>
<span class="lineNum">      76 </span><span class="lineCov">         59 :   if (valid)</span>
<span class="lineNum">      77 </span><span class="lineCov">         54 :     lua_pushinteger(L, n);</span>
<span class="lineNum">      78 </span>            :   else {
<span class="lineNum">      79 </span><span class="lineCov">          5 :     luaL_checkany(L, 1);</span>
<span class="lineNum">      80 </span><span class="lineCov">          5 :     lua_pushnil(L);  /* value is not convertible to integer */</span>
<span class="lineNum">      81 </span>            :   }
<span class="lineNum">      82 </span><span class="lineCov">         59 :   return 1;</span>
<span class="lineNum">      83 </span>            : }
<span class="lineNum">      84 </span>            : 
<span class="lineNum">      85 </span>            : 
<span class="lineNum">      86 </span><span class="lineCov">          7 : static void pushnumint (lua_State *L, lua_Number d) {</span>
<span class="lineNum">      87 </span>            :   lua_Integer n;
<span class="lineNum">      88 </span><span class="lineCov">          7 :   if (lua_numbertointeger(d, &amp;n))  /* does 'd' fit in an integer? */</span>
<span class="lineNum">      89 </span><span class="lineCov">          7 :     lua_pushinteger(L, n);  /* result is integer */</span>
<span class="lineNum">      90 </span>            :   else
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :     lua_pushnumber(L, d);  /* result is float */</span>
<span class="lineNum">      92 </span><span class="lineCov">          7 : }</span>
<span class="lineNum">      93 </span>            : 
<span class="lineNum">      94 </span>            : 
<span class="lineNum">      95 </span><span class="lineCov">          4 : static int math_floor (lua_State *L) {</span>
<span class="lineNum">      96 </span><span class="lineCov">          4 :   if (lua_isinteger(L, 1))</span>
<span class="lineNum">      97 </span><span class="lineCov">          1 :     lua_settop(L, 1);  /* integer is its own floor */</span>
<span class="lineNum">      98 </span>            :   else {
<span class="lineNum">      99 </span><span class="lineCov">          3 :     lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));</span>
<span class="lineNum">     100 </span><span class="lineCov">          3 :     pushnumint(L, d);</span>
<span class="lineNum">     101 </span>            :   }
<span class="lineNum">     102 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">     103 </span>            : }
<span class="lineNum">     104 </span>            : 
<span class="lineNum">     105 </span>            : 
<span class="lineNum">     106 </span><span class="lineCov">          4 : static int math_ceil (lua_State *L) {</span>
<span class="lineNum">     107 </span><span class="lineCov">          4 :   if (lua_isinteger(L, 1))</span>
<span class="lineNum">     108 </span><span class="lineCov">          1 :     lua_settop(L, 1);  /* integer is its own ceil */</span>
<span class="lineNum">     109 </span>            :   else {
<span class="lineNum">     110 </span><span class="lineCov">          3 :     lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));</span>
<span class="lineNum">     111 </span><span class="lineCov">          3 :     pushnumint(L, d);</span>
<span class="lineNum">     112 </span>            :   }
<span class="lineNum">     113 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">     114 </span>            : }
<span class="lineNum">     115 </span>            : 
<span class="lineNum">     116 </span>            : 
<span class="lineNum">     117 </span><span class="lineCov">          9 : static int math_fmod (lua_State *L) {</span>
<span class="lineNum">     118 </span><span class="lineCov">         13 :   if (lua_isinteger(L, 1) &amp;&amp; lua_isinteger(L, 2)) {</span>
<span class="lineNum">     119 </span><span class="lineCov">          5 :     lua_Integer d = lua_tointeger(L, 2);</span>
<span class="lineNum">     120 </span><span class="lineCov">          5 :     if ((lua_Unsigned)d + 1u &lt;= 1u) {  /* special cases: -1 or 0 */</span>
<span class="lineNum">     121 </span><span class="lineCov">          2 :       luaL_argcheck(L, d != 0, 2, &quot;zero&quot;);</span>
<span class="lineNum">     122 </span><span class="lineCov">          1 :       lua_pushinteger(L, 0);  /* avoid overflow with 0x80000... / -1 */</span>
<span class="lineNum">     123 </span>            :     }
<span class="lineNum">     124 </span>            :     else
<span class="lineNum">     125 </span><span class="lineCov">          3 :       lua_pushinteger(L, lua_tointeger(L, 1) % d);</span>
<span class="lineNum">     126 </span>            :   }
<span class="lineNum">     127 </span>            :   else
<span class="lineNum">     128 </span><span class="lineCov">          4 :     lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),</span>
<span class="lineNum">     129 </span>            :                                      luaL_checknumber(L, 2)));
<span class="lineNum">     130 </span><span class="lineCov">          8 :   return 1;</span>
<span class="lineNum">     131 </span>            : }
<span class="lineNum">     132 </span>            : 
<span class="lineNum">     133 </span>            : 
<span class="lineNum">     134 </span>            : /*
<span class="lineNum">     135 </span>            : ** next function does not use 'modf', avoiding problems with 'double*'
<span class="lineNum">     136 </span>            : ** (which is not compatible with 'float*') when lua_Number is not
<span class="lineNum">     137 </span>            : ** 'double'.
<span class="lineNum">     138 </span>            : */
<span class="lineNum">     139 </span><span class="lineCov">          2 : static int math_modf (lua_State *L) {</span>
<span class="lineNum">     140 </span><span class="lineCov">          2 :   if (lua_isinteger(L ,1)) {</span>
<span class="lineNum">     141 </span><span class="lineCov">          1 :     lua_settop(L, 1);  /* number is its own integer part */</span>
<span class="lineNum">     142 </span><span class="lineCov">          1 :     lua_pushnumber(L, 0);  /* no fractional part */</span>
<span class="lineNum">     143 </span>            :   }
<span class="lineNum">     144 </span>            :   else {
<span class="lineNum">     145 </span><span class="lineCov">          1 :     lua_Number n = luaL_checknumber(L, 1);</span>
<span class="lineNum">     146 </span>            :     /* integer part (rounds toward zero) */
<span class="lineNum">     147 </span><span class="lineCov">          1 :     lua_Number ip = (n &lt; 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);</span>
<span class="lineNum">     148 </span><span class="lineCov">          1 :     pushnumint(L, ip);</span>
<span class="lineNum">     149 </span>            :     /* fractional part (test needed for inf/-inf) */
<span class="lineNum">     150 </span><span class="lineCov">          1 :     lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));</span>
<span class="lineNum">     151 </span>            :   }
<span class="lineNum">     152 </span><span class="lineCov">          2 :   return 2;</span>
<span class="lineNum">     153 </span>            : }
<span class="lineNum">     154 </span>            : 
<span class="lineNum">     155 </span>            : 
<span class="lineNum">     156 </span><span class="lineCov">          2 : static int math_sqrt (lua_State *L) {</span>
<span class="lineNum">     157 </span><span class="lineCov">          2 :   lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     158 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     159 </span>            : }
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span>            : 
<span class="lineNum">     162 </span><span class="lineCov">          3 : static int math_ult (lua_State *L) {</span>
<span class="lineNum">     163 </span><span class="lineCov">          3 :   lua_Integer a = luaL_checkinteger(L, 1);</span>
<span class="lineNum">     164 </span><span class="lineCov">          3 :   lua_Integer b = luaL_checkinteger(L, 2);</span>
<span class="lineNum">     165 </span><span class="lineCov">          3 :   lua_pushboolean(L, (lua_Unsigned)a &lt; (lua_Unsigned)b);</span>
<span class="lineNum">     166 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">     167 </span>            : }
<span class="lineNum">     168 </span>            : 
<span class="lineNum">     169 </span><span class="lineCov">          4 : static int math_log (lua_State *L) {</span>
<span class="lineNum">     170 </span><span class="lineCov">          4 :   lua_Number x = luaL_checknumber(L, 1);</span>
<span class="lineNum">     171 </span>            :   lua_Number res;
<span class="lineNum">     172 </span><span class="lineCov">          4 :   if (lua_isnoneornil(L, 2))</span>
<span class="lineNum">     173 </span><span class="lineCov">          1 :     res = l_mathop(log)(x);</span>
<span class="lineNum">     174 </span>            :   else {
<span class="lineNum">     175 </span><span class="lineCov">          3 :     lua_Number base = luaL_checknumber(L, 2);</span>
<span class="lineNum">     176 </span>            : #if !defined(LUA_USE_C89)
<span class="lineNum">     177 </span><span class="lineCov">          3 :     if (base == l_mathop(2.0))</span>
<span class="lineNum">     178 </span><span class="lineCov">          1 :       res = l_mathop(log2)(x); else</span>
<span class="lineNum">     179 </span>            : #endif
<span class="lineNum">     180 </span><span class="lineCov">          2 :     if (base == l_mathop(10.0))</span>
<span class="lineNum">     181 </span><span class="lineCov">          1 :       res = l_mathop(log10)(x);</span>
<span class="lineNum">     182 </span>            :     else
<span class="lineNum">     183 </span><span class="lineCov">          1 :       res = l_mathop(log)(x)/l_mathop(log)(base);</span>
<span class="lineNum">     184 </span>            :   }
<span class="lineNum">     185 </span><span class="lineCov">          4 :   lua_pushnumber(L, res);</span>
<span class="lineNum">     186 </span><span class="lineCov">          4 :   return 1;</span>
<span class="lineNum">     187 </span>            : }
<span class="lineNum">     188 </span>            : 
<span class="lineNum">     189 </span><span class="lineCov">          2 : static int math_exp (lua_State *L) {</span>
<span class="lineNum">     190 </span><span class="lineCov">          2 :   lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     191 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     192 </span>            : }
<span class="lineNum">     193 </span>            : 
<span class="lineNum">     194 </span><span class="lineCov">          1 : static int math_deg (lua_State *L) {</span>
<span class="lineNum">     195 </span><span class="lineCov">          1 :   lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI));</span>
<span class="lineNum">     196 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     197 </span>            : }
<span class="lineNum">     198 </span>            : 
<span class="lineNum">     199 </span><span class="lineCov">          1 : static int math_rad (lua_State *L) {</span>
<span class="lineNum">     200 </span><span class="lineCov">          1 :   lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0)));</span>
<span class="lineNum">     201 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     202 </span>            : }
<span class="lineNum">     203 </span>            : 
<span class="lineNum">     204 </span>            : 
<span class="lineNum">     205 </span><span class="lineCov">          4 : static int math_min (lua_State *L) {</span>
<span class="lineNum">     206 </span><span class="lineCov">          4 :   int n = lua_gettop(L);  /* number of arguments */</span>
<span class="lineNum">     207 </span><span class="lineCov">          4 :   int imin = 1;  /* index of current minimum value */</span>
<span class="lineNum">     208 </span>            :   int i;
<span class="lineNum">     209 </span><span class="lineCov">          4 :   luaL_argcheck(L, n &gt;= 1, 1, &quot;value expected&quot;);</span>
<span class="lineNum">     210 </span><span class="lineCov">          7 :   for (i = 2; i &lt;= n; i++) {</span>
<span class="lineNum">     211 </span><span class="lineCov">          4 :     if (lua_compare(L, i, imin, LUA_OPLT))</span>
<span class="lineNum">     212 </span><span class="lineCov">          1 :       imin = i;</span>
<span class="lineNum">     213 </span>            :   }
<span class="lineNum">     214 </span><span class="lineCov">          3 :   lua_pushvalue(L, imin);</span>
<span class="lineNum">     215 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">     216 </span>            : }
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            : 
<span class="lineNum">     219 </span><span class="lineCov">          6 : static int math_max (lua_State *L) {</span>
<span class="lineNum">     220 </span><span class="lineCov">          6 :   int n = lua_gettop(L);  /* number of arguments */</span>
<span class="lineNum">     221 </span><span class="lineCov">          6 :   int imax = 1;  /* index of current maximum value */</span>
<span class="lineNum">     222 </span>            :   int i;
<span class="lineNum">     223 </span><span class="lineCov">          6 :   luaL_argcheck(L, n &gt;= 1, 1, &quot;value expected&quot;);</span>
<span class="lineNum">     224 </span><span class="lineCov">         13 :   for (i = 2; i &lt;= n; i++) {</span>
<span class="lineNum">     225 </span><span class="lineCov">          8 :     if (lua_compare(L, imax, i, LUA_OPLT))</span>
<span class="lineNum">     226 </span><span class="lineCov">          6 :       imax = i;</span>
<span class="lineNum">     227 </span>            :   }
<span class="lineNum">     228 </span><span class="lineCov">          5 :   lua_pushvalue(L, imax);</span>
<span class="lineNum">     229 </span><span class="lineCov">          5 :   return 1;</span>
<span class="lineNum">     230 </span>            : }
<span class="lineNum">     231 </span>            : 
<span class="lineNum">     232 </span>            : 
<span class="lineNum">     233 </span><span class="lineCov">         15 : static int math_type (lua_State *L) {</span>
<span class="lineNum">     234 </span><span class="lineCov">         15 :   if (lua_type(L, 1) == LUA_TNUMBER)</span>
<span class="lineNum">     235 </span><span class="lineCov">         14 :     lua_pushstring(L, (lua_isinteger(L, 1)) ? &quot;integer&quot; : &quot;float&quot;);</span>
<span class="lineNum">     236 </span>            :   else {
<span class="lineNum">     237 </span><span class="lineCov">          1 :     luaL_checkany(L, 1);</span>
<span class="lineNum">     238 </span><span class="lineCov">          1 :     lua_pushnil(L);</span>
<span class="lineNum">     239 </span>            :   }
<span class="lineNum">     240 </span><span class="lineCov">         15 :   return 1;</span>
<span class="lineNum">     241 </span>            : }
<span class="lineNum">     242 </span>            : 
<span class="lineNum">     243 </span>            : 
<span class="lineNum">     244 </span>            : 
<span class="lineNum">     245 </span>            : /*
<span class="lineNum">     246 </span>            : ** {==================================================================
<span class="lineNum">     247 </span>            : ** Pseudo-Random Number Generator based on 'xoshiro256**'.
<span class="lineNum">     248 </span>            : ** ===================================================================
<span class="lineNum">     249 </span>            : */
<span class="lineNum">     250 </span>            : 
<span class="lineNum">     251 </span>            : /* number of binary digits in the mantissa of a float */
<span class="lineNum">     252 </span>            : #define FIGS    l_mathlim(MANT_DIG)
<span class="lineNum">     253 </span>            : 
<span class="lineNum">     254 </span>            : #if FIGS &gt; 64
<span class="lineNum">     255 </span>            : /* there are only 64 random bits; use them all */
<span class="lineNum">     256 </span>            : #undef FIGS
<span class="lineNum">     257 </span>            : #define FIGS    64
<span class="lineNum">     258 </span>            : #endif
<span class="lineNum">     259 </span>            : 
<span class="lineNum">     260 </span>            : 
<span class="lineNum">     261 </span>            : /*
<span class="lineNum">     262 </span>            : ** LUA_RAND32 forces the use of 32-bit integers in the implementation
<span class="lineNum">     263 </span>            : ** of the PRN generator (mainly for testing).
<span class="lineNum">     264 </span>            : */
<span class="lineNum">     265 </span>            : #if !defined(LUA_RAND32) &amp;&amp; !defined(Rand64)
<span class="lineNum">     266 </span>            : 
<span class="lineNum">     267 </span>            : /* try to find an integer type with at least 64 bits */
<span class="lineNum">     268 </span>            : 
<span class="lineNum">     269 </span>            : #if (ULONG_MAX &gt;&gt; 31 &gt;&gt; 31) &gt;= 3
<span class="lineNum">     270 </span>            : 
<span class="lineNum">     271 </span>            : /* 'long' has at least 64 bits */
<span class="lineNum">     272 </span>            : #define Rand64          unsigned long
<span class="lineNum">     273 </span>            : 
<span class="lineNum">     274 </span>            : #elif !defined(LUA_USE_C89) &amp;&amp; defined(LLONG_MAX)
<span class="lineNum">     275 </span>            : 
<span class="lineNum">     276 </span>            : /* there is a 'long long' type (which must have at least 64 bits) */
<span class="lineNum">     277 </span>            : #define Rand64          unsigned long long
<span class="lineNum">     278 </span>            : 
<span class="lineNum">     279 </span>            : #elif (LUA_MAXUNSIGNED &gt;&gt; 31 &gt;&gt; 31) &gt;= 3
<span class="lineNum">     280 </span>            : 
<span class="lineNum">     281 </span>            : /* 'lua_Integer' has at least 64 bits */
<span class="lineNum">     282 </span>            : #define Rand64          lua_Unsigned
<span class="lineNum">     283 </span>            : 
<span class="lineNum">     284 </span>            : #endif
<span class="lineNum">     285 </span>            : 
<span class="lineNum">     286 </span>            : #endif
<span class="lineNum">     287 </span>            : 
<span class="lineNum">     288 </span>            : 
<span class="lineNum">     289 </span>            : #if defined(Rand64)  /* { */
<span class="lineNum">     290 </span>            : 
<span class="lineNum">     291 </span>            : /*
<span class="lineNum">     292 </span>            : ** Standard implementation, using 64-bit integers.
<span class="lineNum">     293 </span>            : ** If 'Rand64' has more than 64 bits, the extra bits do not interfere
<span class="lineNum">     294 </span>            : ** with the 64 initial bits, except in a right shift. Moreover, the
<span class="lineNum">     295 </span>            : ** final result has to discard the extra bits.
<span class="lineNum">     296 </span>            : */
<span class="lineNum">     297 </span>            : 
<span class="lineNum">     298 </span>            : /* avoid using extra bits when needed */
<span class="lineNum">     299 </span>            : #define trim64(x)       ((x) &amp; 0xffffffffffffffffu)
<span class="lineNum">     300 </span>            : 
<span class="lineNum">     301 </span>            : 
<span class="lineNum">     302 </span>            : /* rotate left 'x' by 'n' bits */
<span class="lineNum">     303 </span><span class="lineCov">       2956 : static Rand64 rotl (Rand64 x, int n) {</span>
<span class="lineNum">     304 </span><span class="lineCov">       2956 :   return (x &lt;&lt; n) | (trim64(x) &gt;&gt; (64 - n));</span>
<span class="lineNum">     305 </span>            : }
<span class="lineNum">     306 </span>            : 
<span class="lineNum">     307 </span><span class="lineCov">       1478 : static Rand64 nextrand (Rand64 *state) {</span>
<span class="lineNum">     308 </span><span class="lineCov">       1478 :   Rand64 state0 = state[0];</span>
<span class="lineNum">     309 </span><span class="lineCov">       1478 :   Rand64 state1 = state[1];</span>
<span class="lineNum">     310 </span><span class="lineCov">       1478 :   Rand64 state2 = state[2] ^ state0;</span>
<span class="lineNum">     311 </span><span class="lineCov">       1478 :   Rand64 state3 = state[3] ^ state1;</span>
<span class="lineNum">     312 </span><span class="lineCov">       1478 :   Rand64 res = rotl(state1 * 5, 7) * 9;</span>
<span class="lineNum">     313 </span><span class="lineCov">       1478 :   state[0] = state0 ^ state3;</span>
<span class="lineNum">     314 </span><span class="lineCov">       1478 :   state[1] = state1 ^ state2;</span>
<span class="lineNum">     315 </span><span class="lineCov">       1478 :   state[2] = state2 ^ (state1 &lt;&lt; 17);</span>
<span class="lineNum">     316 </span><span class="lineCov">       1478 :   state[3] = rotl(state3, 45);</span>
<span class="lineNum">     317 </span><span class="lineCov">       1478 :   return res;</span>
<span class="lineNum">     318 </span>            : }
<span class="lineNum">     319 </span>            : 
<span class="lineNum">     320 </span>            : 
<span class="lineNum">     321 </span>            : /* must take care to not shift stuff by more than 63 slots */
<span class="lineNum">     322 </span>            : 
<span class="lineNum">     323 </span>            : 
<span class="lineNum">     324 </span>            : /*
<span class="lineNum">     325 </span>            : ** Convert bits from a random integer into a float in the
<span class="lineNum">     326 </span>            : ** interval [0,1), getting the higher FIG bits from the
<span class="lineNum">     327 </span>            : ** random unsigned integer and converting that to a float.
<span class="lineNum">     328 </span>            : */
<span class="lineNum">     329 </span>            : 
<span class="lineNum">     330 </span>            : /* must throw out the extra (64 - FIGS) bits */
<span class="lineNum">     331 </span>            : #define shift64_FIG     (64 - FIGS)
<span class="lineNum">     332 </span>            : 
<span class="lineNum">     333 </span>            : /* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */
<span class="lineNum">     334 </span>            : #define scaleFIG        (l_mathop(0.5) / ((Rand64)1 &lt;&lt; (FIGS - 1)))
<span class="lineNum">     335 </span>            : 
<span class="lineNum">     336 </span><span class="lineCov">          4 : static lua_Number I2d (Rand64 x) {</span>
<span class="lineNum">     337 </span><span class="lineCov">          4 :   return (lua_Number)(trim64(x) &gt;&gt; shift64_FIG) * scaleFIG;</span>
<span class="lineNum">     338 </span>            : }
<span class="lineNum">     339 </span>            : 
<span class="lineNum">     340 </span>            : /* convert a 'Rand64' to a 'lua_Unsigned' */
<span class="lineNum">     341 </span>            : #define I2UInt(x)       ((lua_Unsigned)trim64(x))
<span class="lineNum">     342 </span>            : 
<span class="lineNum">     343 </span>            : /* convert a 'lua_Unsigned' to a 'Rand64' */
<span class="lineNum">     344 </span>            : #define Int2I(x)        ((Rand64)(x))
<span class="lineNum">     345 </span>            : 
<span class="lineNum">     346 </span>            : 
<span class="lineNum">     347 </span>            : #else   /* no 'Rand64'   }{ */
<span class="lineNum">     348 </span>            : 
<span class="lineNum">     349 </span>            : /* get an integer with at least 32 bits */
<span class="lineNum">     350 </span>            : #if LUAI_IS32INT
<span class="lineNum">     351 </span>            : typedef unsigned int lu_int32;
<span class="lineNum">     352 </span>            : #else
<span class="lineNum">     353 </span>            : typedef unsigned long lu_int32;
<span class="lineNum">     354 </span>            : #endif
<span class="lineNum">     355 </span>            : 
<span class="lineNum">     356 </span>            : 
<span class="lineNum">     357 </span>            : /*
<span class="lineNum">     358 </span>            : ** Use two 32-bit integers to represent a 64-bit quantity.
<span class="lineNum">     359 </span>            : */
<span class="lineNum">     360 </span>            : typedef struct Rand64 {
<span class="lineNum">     361 </span>            :   lu_int32 h;  /* higher half */
<span class="lineNum">     362 </span>            :   lu_int32 l;  /* lower half */
<span class="lineNum">     363 </span>            : } Rand64;
<span class="lineNum">     364 </span>            : 
<span class="lineNum">     365 </span>            : 
<span class="lineNum">     366 </span>            : /*
<span class="lineNum">     367 </span>            : ** If 'lu_int32' has more than 32 bits, the extra bits do not interfere
<span class="lineNum">     368 </span>            : ** with the 32 initial bits, except in a right shift and comparisons.
<span class="lineNum">     369 </span>            : ** Moreover, the final result has to discard the extra bits.
<span class="lineNum">     370 </span>            : */
<span class="lineNum">     371 </span>            : 
<span class="lineNum">     372 </span>            : /* avoid using extra bits when needed */
<span class="lineNum">     373 </span>            : #define trim32(x)       ((x) &amp; 0xffffffffu)
<span class="lineNum">     374 </span>            : 
<span class="lineNum">     375 </span>            : 
<span class="lineNum">     376 </span>            : /*
<span class="lineNum">     377 </span>            : ** basic operations on 'Rand64' values
<span class="lineNum">     378 </span>            : */
<span class="lineNum">     379 </span>            : 
<span class="lineNum">     380 </span>            : /* build a new Rand64 value */
<span class="lineNum">     381 </span>            : static Rand64 packI (lu_int32 h, lu_int32 l) {
<span class="lineNum">     382 </span>            :   Rand64 result;
<span class="lineNum">     383 </span>            :   result.h = h;
<span class="lineNum">     384 </span>            :   result.l = l;
<span class="lineNum">     385 </span>            :   return result;
<span class="lineNum">     386 </span>            : }
<span class="lineNum">     387 </span>            : 
<span class="lineNum">     388 </span>            : /* return i &lt;&lt; n */
<span class="lineNum">     389 </span>            : static Rand64 Ishl (Rand64 i, int n) {
<span class="lineNum">     390 </span>            :   lua_assert(n &gt; 0 &amp;&amp; n &lt; 32);
<span class="lineNum">     391 </span>            :   return packI((i.h &lt;&lt; n) | (trim32(i.l) &gt;&gt; (32 - n)), i.l &lt;&lt; n);
<span class="lineNum">     392 </span>            : }
<span class="lineNum">     393 </span>            : 
<span class="lineNum">     394 </span>            : /* i1 ^= i2 */
<span class="lineNum">     395 </span>            : static void Ixor (Rand64 *i1, Rand64 i2) {
<span class="lineNum">     396 </span>            :   i1-&gt;h ^= i2.h;
<span class="lineNum">     397 </span>            :   i1-&gt;l ^= i2.l;
<span class="lineNum">     398 </span>            : }
<span class="lineNum">     399 </span>            : 
<span class="lineNum">     400 </span>            : /* return i1 + i2 */
<span class="lineNum">     401 </span>            : static Rand64 Iadd (Rand64 i1, Rand64 i2) {
<span class="lineNum">     402 </span>            :   Rand64 result = packI(i1.h + i2.h, i1.l + i2.l);
<span class="lineNum">     403 </span>            :   if (trim32(result.l) &lt; trim32(i1.l))  /* carry? */
<span class="lineNum">     404 </span>            :     result.h++;
<span class="lineNum">     405 </span>            :   return result;
<span class="lineNum">     406 </span>            : }
<span class="lineNum">     407 </span>            : 
<span class="lineNum">     408 </span>            : /* return i * 5 */
<span class="lineNum">     409 </span>            : static Rand64 times5 (Rand64 i) {
<span class="lineNum">     410 </span>            :   return Iadd(Ishl(i, 2), i);  /* i * 5 == (i &lt;&lt; 2) + i */
<span class="lineNum">     411 </span>            : }
<span class="lineNum">     412 </span>            : 
<span class="lineNum">     413 </span>            : /* return i * 9 */
<span class="lineNum">     414 </span>            : static Rand64 times9 (Rand64 i) {
<span class="lineNum">     415 </span>            :   return Iadd(Ishl(i, 3), i);  /* i * 9 == (i &lt;&lt; 3) + i */
<span class="lineNum">     416 </span>            : }
<span class="lineNum">     417 </span>            : 
<span class="lineNum">     418 </span>            : /* return 'i' rotated left 'n' bits */
<span class="lineNum">     419 </span>            : static Rand64 rotl (Rand64 i, int n) {
<span class="lineNum">     420 </span>            :   lua_assert(n &gt; 0 &amp;&amp; n &lt; 32);
<span class="lineNum">     421 </span>            :   return packI((i.h &lt;&lt; n) | (trim32(i.l) &gt;&gt; (32 - n)),
<span class="lineNum">     422 </span>            :                (trim32(i.h) &gt;&gt; (32 - n)) | (i.l &lt;&lt; n));
<span class="lineNum">     423 </span>            : }
<span class="lineNum">     424 </span>            : 
<span class="lineNum">     425 </span>            : /* for offsets larger than 32, rotate right by 64 - offset */
<span class="lineNum">     426 </span>            : static Rand64 rotl1 (Rand64 i, int n) {
<span class="lineNum">     427 </span>            :   lua_assert(n &gt; 32 &amp;&amp; n &lt; 64);
<span class="lineNum">     428 </span>            :   n = 64 - n;
<span class="lineNum">     429 </span>            :   return packI((trim32(i.h) &gt;&gt; n) | (i.l &lt;&lt; (32 - n)),
<span class="lineNum">     430 </span>            :                (i.h &lt;&lt; (32 - n)) | (trim32(i.l) &gt;&gt; n));
<span class="lineNum">     431 </span>            : }
<span class="lineNum">     432 </span>            : 
<span class="lineNum">     433 </span>            : /*
<span class="lineNum">     434 </span>            : ** implementation of 'xoshiro256**' algorithm on 'Rand64' values
<span class="lineNum">     435 </span>            : */
<span class="lineNum">     436 </span>            : static Rand64 nextrand (Rand64 *state) {
<span class="lineNum">     437 </span>            :   Rand64 res = times9(rotl(times5(state[1]), 7));
<span class="lineNum">     438 </span>            :   Rand64 t = Ishl(state[1], 17);
<span class="lineNum">     439 </span>            :   Ixor(&amp;state[2], state[0]);
<span class="lineNum">     440 </span>            :   Ixor(&amp;state[3], state[1]);
<span class="lineNum">     441 </span>            :   Ixor(&amp;state[1], state[2]);
<span class="lineNum">     442 </span>            :   Ixor(&amp;state[0], state[3]);
<span class="lineNum">     443 </span>            :   Ixor(&amp;state[2], t);
<span class="lineNum">     444 </span>            :   state[3] = rotl1(state[3], 45);
<span class="lineNum">     445 </span>            :   return res;
<span class="lineNum">     446 </span>            : }
<span class="lineNum">     447 </span>            : 
<span class="lineNum">     448 </span>            : 
<span class="lineNum">     449 </span>            : /*
<span class="lineNum">     450 </span>            : ** Converts a 'Rand64' into a float.
<span class="lineNum">     451 </span>            : */
<span class="lineNum">     452 </span>            : 
<span class="lineNum">     453 </span>            : /* an unsigned 1 with proper type */
<span class="lineNum">     454 </span>            : #define UONE            ((lu_int32)1)
<span class="lineNum">     455 </span>            : 
<span class="lineNum">     456 </span>            : 
<span class="lineNum">     457 </span>            : #if FIGS &lt;= 32
<span class="lineNum">     458 </span>            : 
<span class="lineNum">     459 </span>            : /* 2^(-FIGS) */
<span class="lineNum">     460 </span>            : #define scaleFIG       (l_mathop(0.5) / (UONE &lt;&lt; (FIGS - 1)))
<span class="lineNum">     461 </span>            : 
<span class="lineNum">     462 </span>            : /*
<span class="lineNum">     463 </span>            : ** get up to 32 bits from higher half, shifting right to
<span class="lineNum">     464 </span>            : ** throw out the extra bits.
<span class="lineNum">     465 </span>            : */
<span class="lineNum">     466 </span>            : static lua_Number I2d (Rand64 x) {
<span class="lineNum">     467 </span>            :   lua_Number h = (lua_Number)(trim32(x.h) &gt;&gt; (32 - FIGS));
<span class="lineNum">     468 </span>            :   return h * scaleFIG;
<span class="lineNum">     469 </span>            : }
<span class="lineNum">     470 </span>            : 
<span class="lineNum">     471 </span>            : #else   /* 32 &lt; FIGS &lt;= 64 */
<span class="lineNum">     472 </span>            : 
<span class="lineNum">     473 </span>            : /* must take care to not shift stuff by more than 31 slots */
<span class="lineNum">     474 </span>            : 
<span class="lineNum">     475 </span>            : /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */
<span class="lineNum">     476 </span>            : #define scaleFIG  \
<span class="lineNum">     477 </span>            :         ((lua_Number)1.0 / (UONE &lt;&lt; 30) / 8.0 / (UONE &lt;&lt; (FIGS - 33)))
<span class="lineNum">     478 </span>            : 
<span class="lineNum">     479 </span>            : /*
<span class="lineNum">     480 </span>            : ** use FIGS - 32 bits from lower half, throwing out the other
<span class="lineNum">     481 </span>            : ** (32 - (FIGS - 32)) = (64 - FIGS) bits
<span class="lineNum">     482 </span>            : */
<span class="lineNum">     483 </span>            : #define shiftLOW        (64 - FIGS)
<span class="lineNum">     484 </span>            : 
<span class="lineNum">     485 </span>            : /*
<span class="lineNum">     486 </span>            : ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32)
<span class="lineNum">     487 </span>            : */
<span class="lineNum">     488 </span>            : #define shiftHI         ((lua_Number)(UONE &lt;&lt; (FIGS - 33)) * 2.0)
<span class="lineNum">     489 </span>            : 
<span class="lineNum">     490 </span>            : 
<span class="lineNum">     491 </span>            : static lua_Number I2d (Rand64 x) {
<span class="lineNum">     492 </span>            :   lua_Number h = (lua_Number)trim32(x.h) * shiftHI;
<span class="lineNum">     493 </span>            :   lua_Number l = (lua_Number)(trim32(x.l) &gt;&gt; shiftLOW);
<span class="lineNum">     494 </span>            :   return (h + l) * scaleFIG;
<span class="lineNum">     495 </span>            : }
<span class="lineNum">     496 </span>            : 
<span class="lineNum">     497 </span>            : #endif
<span class="lineNum">     498 </span>            : 
<span class="lineNum">     499 </span>            : 
<span class="lineNum">     500 </span>            : /* convert a 'Rand64' to a 'lua_Unsigned' */
<span class="lineNum">     501 </span>            : static lua_Unsigned I2UInt (Rand64 x) {
<span class="lineNum">     502 </span>            :   return ((lua_Unsigned)trim32(x.h) &lt;&lt; 31 &lt;&lt; 1) | (lua_Unsigned)trim32(x.l);
<span class="lineNum">     503 </span>            : }
<span class="lineNum">     504 </span>            : 
<span class="lineNum">     505 </span>            : /* convert a 'lua_Unsigned' to a 'Rand64' */
<span class="lineNum">     506 </span>            : static Rand64 Int2I (lua_Unsigned n) {
<span class="lineNum">     507 </span>            :   return packI((lu_int32)(n &gt;&gt; 31 &gt;&gt; 1), (lu_int32)n);
<span class="lineNum">     508 </span>            : }
<span class="lineNum">     509 </span>            : 
<span class="lineNum">     510 </span>            : #endif  /* } */
<span class="lineNum">     511 </span>            : 
<span class="lineNum">     512 </span>            : 
<span class="lineNum">     513 </span>            : /*
<span class="lineNum">     514 </span>            : ** A state uses four 'Rand64' values.
<span class="lineNum">     515 </span>            : */
<span class="lineNum">     516 </span>            : typedef struct {
<span class="lineNum">     517 </span>            :   Rand64 s[4];
<span class="lineNum">     518 </span>            : } RanState;
<span class="lineNum">     519 </span>            : 
<span class="lineNum">     520 </span>            : 
<span class="lineNum">     521 </span>            : /*
<span class="lineNum">     522 </span>            : ** Project the random integer 'ran' into the interval [0, n].
<span class="lineNum">     523 </span>            : ** Because 'ran' has 2^B possible values, the projection can only be
<span class="lineNum">     524 </span>            : ** uniform when the size of the interval is a power of 2 (exact
<span class="lineNum">     525 </span>            : ** division).  To get a uniform projection into [0, n], we first compute
<span class="lineNum">     526 </span>            : ** 'lim', the smallest Mersenne number not smaller than 'n'. We then
<span class="lineNum">     527 </span>            : ** project 'ran' into the interval [0, lim].  If the result is inside
<span class="lineNum">     528 </span>            : ** [0, n], we are done. Otherwise, we try with another 'ran', until we
<span class="lineNum">     529 </span>            : ** have a result inside the interval.
<span class="lineNum">     530 </span>            : */
<span class="lineNum">     531 </span><span class="lineCov">          5 : static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n,</span>
<span class="lineNum">     532 </span>            :                              RanState *state) {
<span class="lineNum">     533 </span><span class="lineCov">          5 :   lua_Unsigned lim = n;</span>
<span class="lineNum">     534 </span><span class="lineCov">          5 :   if ((lim &amp; (lim + 1)) &gt; 0) {  /* 'lim + 1' is not a power of 2? */</span>
<span class="lineNum">     535 </span>            :     /* compute the smallest (2^b - 1) not smaller than 'n' */
<span class="lineNum">     536 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 1);</span>
<span class="lineNum">     537 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 2);</span>
<span class="lineNum">     538 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 4);</span>
<span class="lineNum">     539 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 8);</span>
<span class="lineNum">     540 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 16);</span>
<span class="lineNum">     541 </span>            : #if (LUA_MAXUNSIGNED &gt;&gt; 31) &gt;= 3
<span class="lineNum">     542 </span><span class="lineCov">          5 :     lim |= (lim &gt;&gt; 32);  /* integer type has more than 32 bits */</span>
<span class="lineNum">     543 </span>            : #endif
<span class="lineNum">     544 </span>            :   }
<span class="lineNum">     545 </span>            :   lua_assert((lim &amp; (lim + 1)) == 0  /* 'lim + 1' is a power of 2, */
<span class="lineNum">     546 </span>            :     &amp;&amp; lim &gt;= n  /* not smaller than 'n', */
<span class="lineNum">     547 </span>            :     &amp;&amp; (lim == 0 || (lim &gt;&gt; 1) &lt; n));  /* and it is the smallest one */
<span class="lineNum">     548 </span><span class="lineCov">         14 :   while ((ran &amp;= lim) &gt; n)  /* project 'ran' into [0..lim] */</span>
<span class="lineNum">     549 </span><span class="lineCov">          9 :     ran = I2UInt(nextrand(state-&gt;s));  /* not inside [0..n]? try again */</span>
<span class="lineNum">     550 </span><span class="lineCov">          5 :   return ran;</span>
<span class="lineNum">     551 </span>            : }
<span class="lineNum">     552 </span>            : 
<span class="lineNum">     553 </span>            : 
<span class="lineNum">     554 </span><span class="lineCov">         13 : static int math_random (lua_State *L) {</span>
<span class="lineNum">     555 </span>            :   lua_Integer low, up;
<span class="lineNum">     556 </span>            :   lua_Unsigned p;
<span class="lineNum">     557 </span><span class="lineCov">         13 :   RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));</span>
<span class="lineNum">     558 </span><span class="lineCov">         13 :   Rand64 rv = nextrand(state-&gt;s);  /* next pseudo-random value */</span>
<span class="lineNum">     559 </span><span class="lineCov">         13 :   switch (lua_gettop(L)) {  /* check number of arguments */</span>
<span class="lineNum">     560 </span><span class="lineCov">          4 :     case 0: {  /* no arguments */</span>
<span class="lineNum">     561 </span><span class="lineCov">          4 :       lua_pushnumber(L, I2d(rv));  /* float between 0 and 1 */</span>
<span class="lineNum">     562 </span><span class="lineCov">          4 :       return 1;</span>
<span class="lineNum">     563 </span>            :     }
<span class="lineNum">     564 </span><span class="lineCov">          4 :     case 1: {  /* only upper limit */</span>
<span class="lineNum">     565 </span><span class="lineCov">          4 :       low = 1;</span>
<span class="lineNum">     566 </span><span class="lineCov">          4 :       up = luaL_checkinteger(L, 1);</span>
<span class="lineNum">     567 </span><span class="lineCov">          4 :       if (up == 0) {  /* single 0 as argument? */</span>
<span class="lineNum">     568 </span><span class="lineCov">          1 :         lua_pushinteger(L, I2UInt(rv));  /* full random integer */</span>
<span class="lineNum">     569 </span><span class="lineCov">          1 :         return 1;</span>
<span class="lineNum">     570 </span>            :       }
<span class="lineNum">     571 </span><span class="lineCov">          3 :       break;</span>
<span class="lineNum">     572 </span>            :     }
<span class="lineNum">     573 </span><span class="lineCov">          4 :     case 2: {  /* lower and upper limits */</span>
<span class="lineNum">     574 </span><span class="lineCov">          4 :       low = luaL_checkinteger(L, 1);</span>
<span class="lineNum">     575 </span><span class="lineCov">          4 :       up = luaL_checkinteger(L, 2);</span>
<span class="lineNum">     576 </span><span class="lineCov">          4 :       break;</span>
<span class="lineNum">     577 </span>            :     }
<span class="lineNum">     578 </span><span class="lineCov">          1 :     default: return luaL_error(L, &quot;wrong number of arguments&quot;);</span>
<span class="lineNum">     579 </span>            :   }
<span class="lineNum">     580 </span>            :   /* random integer in the interval [low, up] */
<span class="lineNum">     581 </span><span class="lineCov">          7 :   luaL_argcheck(L, low &lt;= up, 1, &quot;interval is empty&quot;);</span>
<span class="lineNum">     582 </span>            :   /* project random integer into the interval [0, up - low] */
<span class="lineNum">     583 </span><span class="lineCov">          5 :   p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);</span>
<span class="lineNum">     584 </span><span class="lineCov">          5 :   lua_pushinteger(L, p + (lua_Unsigned)low);</span>
<span class="lineNum">     585 </span><span class="lineCov">          5 :   return 1;</span>
<span class="lineNum">     586 </span>            : }
<span class="lineNum">     587 </span>            : 
<span class="lineNum">     588 </span>            : 
<span class="lineNum">     589 </span><span class="lineCov">         91 : static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) {</span>
<span class="lineNum">     590 </span>            :   int i;
<span class="lineNum">     591 </span><span class="lineCov">         91 :   state[0] = Int2I(n1);</span>
<span class="lineNum">     592 </span><span class="lineCov">         91 :   state[1] = Int2I(0xff);  /* avoid a zero state */</span>
<span class="lineNum">     593 </span><span class="lineCov">         91 :   state[2] = Int2I(n2);</span>
<span class="lineNum">     594 </span><span class="lineCov">         91 :   state[3] = Int2I(0);</span>
<span class="lineNum">     595 </span><span class="lineCov">       1547 :   for (i = 0; i &lt; 16; i++)</span>
<span class="lineNum">     596 </span><span class="lineCov">       1456 :     nextrand(state);  /* discard initial values to &quot;spread&quot; seed */</span>
<span class="lineNum">     597 </span><span class="lineCov">         91 : }</span>
<span class="lineNum">     598 </span>            : 
<span class="lineNum">     599 </span>            : 
<span class="lineNum">     600 </span>            : /*
<span class="lineNum">     601 </span>            : ** Set a &quot;random&quot; seed. To get some randomness, use the current time
<span class="lineNum">     602 </span>            : ** and the address of 'L' (in case the machine does address space layout
<span class="lineNum">     603 </span>            : ** randomization).
<span class="lineNum">     604 </span>            : */
<span class="lineNum">     605 </span><span class="lineCov">         89 : static void randseed (lua_State *L, RanState *state) {</span>
<span class="lineNum">     606 </span><span class="lineCov">         89 :   lua_Unsigned seed1 = (lua_Unsigned)time(NULL);</span>
<span class="lineNum">     607 </span><span class="lineCov">         89 :   lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;</span>
<span class="lineNum">     608 </span><span class="lineCov">         89 :   setseed(state-&gt;s, seed1, seed2);</span>
<span class="lineNum">     609 </span><span class="lineCov">         89 : }</span>
<span class="lineNum">     610 </span>            : 
<span class="lineNum">     611 </span>            : 
<span class="lineNum">     612 </span><span class="lineCov">          2 : static int math_randomseed (lua_State *L) {</span>
<span class="lineNum">     613 </span><span class="lineCov">          2 :   RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));</span>
<span class="lineNum">     614 </span><span class="lineCov">          2 :   if (lua_isnone(L, 1))</span>
<span class="lineNum">     615 </span><span class="lineNoCov">          0 :     randseed(L, state);</span>
<span class="lineNum">     616 </span>            :   else {
<span class="lineNum">     617 </span><span class="lineCov">          2 :     lua_Integer n1 = luaL_checkinteger(L, 1);</span>
<span class="lineNum">     618 </span><span class="lineCov">          2 :     lua_Integer n2 = luaL_optinteger(L, 2, 0);</span>
<span class="lineNum">     619 </span><span class="lineCov">          2 :     setseed(state-&gt;s, n1, n2);</span>
<span class="lineNum">     620 </span>            :   }
<span class="lineNum">     621 </span><span class="lineCov">          2 :   return 0;</span>
<span class="lineNum">     622 </span>            : }
<span class="lineNum">     623 </span>            : 
<span class="lineNum">     624 </span>            : 
<span class="lineNum">     625 </span>            : static const luaL_Reg randfuncs[] = {
<span class="lineNum">     626 </span>            :   {&quot;random&quot;, math_random},
<span class="lineNum">     627 </span>            :   {&quot;randomseed&quot;, math_randomseed},
<span class="lineNum">     628 </span>            :   {NULL, NULL}
<span class="lineNum">     629 </span>            : };
<span class="lineNum">     630 </span>            : 
<span class="lineNum">     631 </span>            : 
<span class="lineNum">     632 </span>            : /*
<span class="lineNum">     633 </span>            : ** Register the random functions and initialize their state.
<span class="lineNum">     634 </span>            : */
<span class="lineNum">     635 </span><span class="lineCov">         89 : static void setrandfunc (lua_State *L) {</span>
<span class="lineNum">     636 </span><span class="lineCov">         89 :   RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);</span>
<span class="lineNum">     637 </span><span class="lineCov">         89 :   randseed(L, state);  /* initialize with a &quot;random&quot; seed */</span>
<span class="lineNum">     638 </span><span class="lineCov">         89 :   luaL_setfuncs(L, randfuncs, 1);</span>
<span class="lineNum">     639 </span><span class="lineCov">         89 : }</span>
<span class="lineNum">     640 </span>            : 
<span class="lineNum">     641 </span>            : /* }================================================================== */
<span class="lineNum">     642 </span>            : 
<span class="lineNum">     643 </span>            : 
<span class="lineNum">     644 </span>            : /*
<span class="lineNum">     645 </span>            : ** {==================================================================
<span class="lineNum">     646 </span>            : ** Deprecated functions (for compatibility only)
<span class="lineNum">     647 </span>            : ** ===================================================================
<span class="lineNum">     648 </span>            : */
<span class="lineNum">     649 </span>            : #if defined(LUA_COMPAT_MATHLIB)
<span class="lineNum">     650 </span>            : 
<span class="lineNum">     651 </span><span class="lineCov">          1 : static int math_cosh (lua_State *L) {</span>
<span class="lineNum">     652 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     653 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     654 </span>            : }
<span class="lineNum">     655 </span>            : 
<span class="lineNum">     656 </span><span class="lineCov">          1 : static int math_sinh (lua_State *L) {</span>
<span class="lineNum">     657 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     658 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     659 </span>            : }
<span class="lineNum">     660 </span>            : 
<span class="lineNum">     661 </span><span class="lineCov">          1 : static int math_tanh (lua_State *L) {</span>
<span class="lineNum">     662 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     663 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     664 </span>            : }
<span class="lineNum">     665 </span>            : 
<span class="lineNum">     666 </span><span class="lineCov">          1 : static int math_pow (lua_State *L) {</span>
<span class="lineNum">     667 </span><span class="lineCov">          1 :   lua_Number x = luaL_checknumber(L, 1);</span>
<span class="lineNum">     668 </span><span class="lineCov">          1 :   lua_Number y = luaL_checknumber(L, 2);</span>
<span class="lineNum">     669 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(pow)(x, y));</span>
<span class="lineNum">     670 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     671 </span>            : }
<span class="lineNum">     672 </span>            : 
<span class="lineNum">     673 </span><span class="lineCov">          1 : static int math_frexp (lua_State *L) {</span>
<span class="lineNum">     674 </span>            :   int e;
<span class="lineNum">     675 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &amp;e));</span>
<span class="lineNum">     676 </span><span class="lineCov">          1 :   lua_pushinteger(L, e);</span>
<span class="lineNum">     677 </span><span class="lineCov">          1 :   return 2;</span>
<span class="lineNum">     678 </span>            : }
<span class="lineNum">     679 </span>            : 
<span class="lineNum">     680 </span><span class="lineCov">          1 : static int math_ldexp (lua_State *L) {</span>
<span class="lineNum">     681 </span><span class="lineCov">          1 :   lua_Number x = luaL_checknumber(L, 1);</span>
<span class="lineNum">     682 </span><span class="lineCov">          1 :   int ep = (int)luaL_checkinteger(L, 2);</span>
<span class="lineNum">     683 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(ldexp)(x, ep));</span>
<span class="lineNum">     684 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     685 </span>            : }
<span class="lineNum">     686 </span>            : 
<span class="lineNum">     687 </span><span class="lineCov">          1 : static int math_log10 (lua_State *L) {</span>
<span class="lineNum">     688 </span><span class="lineCov">          1 :   lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1)));</span>
<span class="lineNum">     689 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     690 </span>            : }
<span class="lineNum">     691 </span>            : 
<span class="lineNum">     692 </span>            : #endif
<span class="lineNum">     693 </span>            : /* }================================================================== */
<span class="lineNum">     694 </span>            : 
<span class="lineNum">     695 </span>            : 
<span class="lineNum">     696 </span>            : 
<span class="lineNum">     697 </span>            : static const luaL_Reg mathlib[] = {
<span class="lineNum">     698 </span>            :   {&quot;abs&quot;,   math_abs},
<span class="lineNum">     699 </span>            :   {&quot;acos&quot;,  math_acos},
<span class="lineNum">     700 </span>            :   {&quot;asin&quot;,  math_asin},
<span class="lineNum">     701 </span>            :   {&quot;atan&quot;,  math_atan},
<span class="lineNum">     702 </span>            :   {&quot;ceil&quot;,  math_ceil},
<span class="lineNum">     703 </span>            :   {&quot;cos&quot;,   math_cos},
<span class="lineNum">     704 </span>            :   {&quot;deg&quot;,   math_deg},
<span class="lineNum">     705 </span>            :   {&quot;exp&quot;,   math_exp},
<span class="lineNum">     706 </span>            :   {&quot;tointeger&quot;, math_toint},
<span class="lineNum">     707 </span>            :   {&quot;floor&quot;, math_floor},
<span class="lineNum">     708 </span>            :   {&quot;fmod&quot;,   math_fmod},
<span class="lineNum">     709 </span>            :   {&quot;ult&quot;,   math_ult},
<span class="lineNum">     710 </span>            :   {&quot;log&quot;,   math_log},
<span class="lineNum">     711 </span>            :   {&quot;max&quot;,   math_max},
<span class="lineNum">     712 </span>            :   {&quot;min&quot;,   math_min},
<span class="lineNum">     713 </span>            :   {&quot;modf&quot;,   math_modf},
<span class="lineNum">     714 </span>            :   {&quot;rad&quot;,   math_rad},
<span class="lineNum">     715 </span>            :   {&quot;sin&quot;,   math_sin},
<span class="lineNum">     716 </span>            :   {&quot;sqrt&quot;,  math_sqrt},
<span class="lineNum">     717 </span>            :   {&quot;tan&quot;,   math_tan},
<span class="lineNum">     718 </span>            :   {&quot;type&quot;, math_type},
<span class="lineNum">     719 </span>            : #if defined(LUA_COMPAT_MATHLIB)
<span class="lineNum">     720 </span>            :   {&quot;atan2&quot;, math_atan},
<span class="lineNum">     721 </span>            :   {&quot;cosh&quot;,   math_cosh},
<span class="lineNum">     722 </span>            :   {&quot;sinh&quot;,   math_sinh},
<span class="lineNum">     723 </span>            :   {&quot;tanh&quot;,   math_tanh},
<span class="lineNum">     724 </span>            :   {&quot;pow&quot;,   math_pow},
<span class="lineNum">     725 </span>            :   {&quot;frexp&quot;, math_frexp},
<span class="lineNum">     726 </span>            :   {&quot;ldexp&quot;, math_ldexp},
<span class="lineNum">     727 </span>            :   {&quot;log10&quot;, math_log10},
<span class="lineNum">     728 </span>            : #endif
<span class="lineNum">     729 </span>            :   /* placeholders */
<span class="lineNum">     730 </span>            :   {&quot;random&quot;, NULL},
<span class="lineNum">     731 </span>            :   {&quot;randomseed&quot;, NULL},
<span class="lineNum">     732 </span>            :   {&quot;pi&quot;, NULL},
<span class="lineNum">     733 </span>            :   {&quot;huge&quot;, NULL},
<span class="lineNum">     734 </span>            :   {&quot;maxinteger&quot;, NULL},
<span class="lineNum">     735 </span>            :   {&quot;mininteger&quot;, NULL},
<span class="lineNum">     736 </span>            :   {NULL, NULL}
<span class="lineNum">     737 </span>            : };
<span class="lineNum">     738 </span>            : 
<span class="lineNum">     739 </span>            : 
<span class="lineNum">     740 </span>            : /*
<span class="lineNum">     741 </span>            : ** Open math library
<span class="lineNum">     742 </span>            : */
<span class="lineNum">     743 </span><span class="lineCov">         89 : LUAMOD_API int luaopen_math (lua_State *L) {</span>
<span class="lineNum">     744 </span><span class="lineCov">         89 :   luaL_newlib(L, mathlib);</span>
<span class="lineNum">     745 </span><span class="lineCov">         89 :   lua_pushnumber(L, PI);</span>
<span class="lineNum">     746 </span><span class="lineCov">         89 :   lua_setfield(L, -2, &quot;pi&quot;);</span>
<span class="lineNum">     747 </span><span class="lineCov">         89 :   lua_pushnumber(L, (lua_Number)HUGE_VAL);</span>
<span class="lineNum">     748 </span><span class="lineCov">         89 :   lua_setfield(L, -2, &quot;huge&quot;);</span>
<span class="lineNum">     749 </span><span class="lineCov">         89 :   lua_pushinteger(L, LUA_MAXINTEGER);</span>
<span class="lineNum">     750 </span><span class="lineCov">         89 :   lua_setfield(L, -2, &quot;maxinteger&quot;);</span>
<span class="lineNum">     751 </span><span class="lineCov">         89 :   lua_pushinteger(L, LUA_MININTEGER);</span>
<span class="lineNum">     752 </span><span class="lineCov">         89 :   lua_setfield(L, -2, &quot;mininteger&quot;);</span>
<span class="lineNum">     753 </span><span class="lineCov">         89 :   setrandfunc(L);</span>
<span class="lineNum">     754 </span><span class="lineCov">         89 :   return 1;</span>
<span class="lineNum">     755 </span>            : }
<span class="lineNum">     756 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
